package top.cross.webapp.common;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.rule.ActivityTestRule;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

import top.cross.webapp.common.plugin.CheckNetSpeed;
import top.cross.webapp.common.webview.WebViewActivity;
import top.crossrun.webview.bridge.core.pls.BridgeCallBackFunction;
import top.crossrun.webview.bridge.core.pls.WebViewBridgePlugin;
import top.crossrun.webview.bridge.core.pls.WebViewBridgePluginManagerAdapter;
import top.crossrun.webview.bridge.core.utils.RawJSEval;

import static org.junit.Assert.assertEquals;

@RunWith(AndroidJUnit4.class)
public class ADEEInstrumentedTest {

    public ADEEInstrumentedTest() {
//        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
//        Intent intent = new Intent(appContext, WebViewActivity.class);
//        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//
//        ActivityTestRule<WebViewActivity> activityTestRule = new ActivityTestRule<>(WebViewActivity.class, false, false);
//        activity = activityTestRule.launchActivity(intent);
//
//        adapter = new WebViewBridgePluginManagerAdapter(new RawJSEval(null)) {
//            @Override
//            public Activity getActivityContext() {
//                try {
//                    return activity;
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//                return null;
//            }
//        };
    }

    @Test
    public void DSFAppContext() {

        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
        Intent intent = new Intent(appContext, WebViewActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        ActivityTestRule<WebViewActivity> activityTestRule = new ActivityTestRule<>(WebViewActivity.class, false, false);
        final Activity activity = activityTestRule.launchActivity(intent);

        WebViewBridgePluginManagerAdapter adapter = new WebViewBridgePluginManagerAdapter(new RawJSEval(null)) {
            @Override
            public Activity getActivityContext() {
                try {
                    return activity;
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
        };

        WebViewBridgePlugin bridge = new TestBridgePlugin();

        adapter.pluginRegister(bridge);

        testBridgeWithouThread(bridge, "dafads", new BridgeCallBackFunction() {
            @Override
            public void onCallBackFunction(String msg) {
                log(msg);
//                assertEquals(msg, activity.getPackageName());
            }
        });

//        assertEquals("top.cross.webapp.common", appContext.getPackageName());
    }


    @Test
    public void testCheckNetSpeed() throws InterruptedException {

        final AtomicInteger lock = new AtomicInteger(0);

        final CountDownLatch countDownLatch = new CountDownLatch(1);

        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
        Intent intent = new Intent(appContext, WebViewActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        ActivityTestRule<WebViewActivity> activityTestRule = new ActivityTestRule<>(WebViewActivity.class, false, false);
        final Activity activity = activityTestRule.launchActivity(intent);

        final WebViewBridgePluginManagerAdapter adapter = new WebViewBridgePluginManagerAdapter(new RawJSEval(null)) {
            @Override
            public Activity getActivityContext() {
                try {
                    return activity;
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
        };

        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                WebViewBridgePlugin bridge = new CheckNetSpeed();

                adapter.pluginRegister(bridge);

                testBridgeWithouThread(bridge, "dafads", new BridgeCallBackFunction() {
                    @Override
                    public void onCallBackFunction(String msg) {
                        log("结果" + msg);
//                        lock.addAndGet(1);
                        countDownLatch.countDown();
                    }
                });
            }
        });
        countDownLatch.await();
//        while (lock.get() == 0) {
//            Thread.sleep(1 * 1000);
//        }
    }

    private void testBridgeWithouThread(WebViewBridgePlugin wbp, String msg, BridgeCallBackFunction callBackFunction) {
        wbp._handlerBridgeMsg(msg, callBackFunction);
    }

    private static void log(String msg) {
        Log.e("android_test", msg);
    }
}
